import { _decorator, Component, instantiate, Label, Node } from 'cc';
import { Player } from './tank/Player';
import { myGame } from './MyGame';
const { ccclass, property } = _decorator;

@ccclass('UI_Layer')
export class UI_Layer extends Component {
    @property(Node)
    public btnShoot: Node = null

    @property(Node)
    public enemyInfo: Node = null

    @property(Label)
    public labelLv: Label = null

    
    @property(Label)
    public labelLife: Label = null

    onLoad() {
        this.btnShoot.on(Node.EventType.TOUCH_START, this.onBtnShoot, this)
    }

    onBtnShoot() {
        if (myGame.player) {
            myGame.player.shoot()
        }
    }

    updateEnemyNum() {
        const restNum = myGame.maxEnemyNum - myGame.enemyDieNum
        const children = this.enemyInfo.children
        const childClone = children[0]
        for (let i = 0; i < restNum; i++) {
            let child = children[i]
            if (!child) {
                child = instantiate(childClone)
                child.parent = this.enemyInfo
            }
            child.active = true
        }

        for (let i = children.length - 1; i >= restNum; i--) {
            let child = children[i]
            if (i == 0) {
                child.active = false
            }
            else {
                child.removeFromParent()
                child.destroy()
            }
        }
    }

    updateLevel() {
        this.labelLv.string = myGame.level.toString()
    }

    updatePlayerLife() {
        this.labelLife.string = myGame.player.life.toString()
    }
}


